package cc.rengu.redp.bizimpl.csmc.service.impl;

import cc.rengu.redp.bizimpl.csmc.dao.TxnStatisMapper;
import cc.rengu.redp.bizimpl.csmc.domain.TxnStatis;
import cc.rengu.redp.bizimpl.csmc.service.ITxnStatisService;
import cc.rengu.redp.bizimpl.upmp.domain.UpmpUserInfo;
import cc.rengu.redp.bizimpl.upmp.service.IUpmpUserInfoService;
import cc.rengu.redp.bizimpl.utils.DateUtils;
import cc.rengu.redp.common.exception.RedpException;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author guohaocong
 */
@Service
@Slf4j
public class TxnStatisServiceImpl extends ServiceImpl<TxnStatisMapper, TxnStatis> implements ITxnStatisService {

    @Autowired
    private IUpmpUserInfoService upmpUserInfoService;

    @Override
    public Map<String, Object> getTxnStatis(String userId) throws RedpException {
        String yestdate = DateUtils.formatDate(DateUtils.nDaysAgo(1, new Date()), "yyyyMMdd");
        String ndaysAgo = DateUtils.formatDate(DateUtils.nDaysAgo(7, new Date()), "yyyyMMdd");
        UpmpUserInfo userInfo = upmpUserInfoService.findById(userId);
        String userOrgCode = userInfo.getOrgCode();
        Map<String, Object> map = new HashMap<>();
        //查询交易统计表
        List<Map> list = null;
        try {
            list = this.baseMapper.findNoDateTransCounts(userOrgCode, yestdate);
        } catch (Exception e) {
            String message = "查询交易统计表出现异常";
            log.error(message);
            throw new RedpException(message);
        }
        if (list.size() == 0) {
            map.put("yestdayTrans", null);
        } else {
            map.put("yestdayTrans", list);
        }

        List<Map> listDate = null;
        try {
            listDate = this.baseMapper.findDateTransCounts(userOrgCode, ndaysAgo, yestdate);
        } catch (Exception e) {
            String message = "查询统计日期的交易统计表出现异常";
            log.error(message);
            throw new RedpException(message);
        }
        String[] txnDate = {DateUtils.formatDate(DateUtils.nDaysAgo(7, new Date()), "yyyyMMdd"), DateUtils.formatDate(DateUtils.nDaysAgo(6, new Date()), "yyyyMMdd"), DateUtils.formatDate(DateUtils.nDaysAgo(5, new Date()), "yyyyMMdd"), DateUtils.formatDate(DateUtils.nDaysAgo(4, new Date()), "yyyyMMdd"), DateUtils.formatDate(DateUtils.nDaysAgo(3, new Date()), "yyyyMMdd"), DateUtils.formatDate(DateUtils.nDaysAgo(2, new Date()), "yyyyMMdd"), DateUtils.formatDate(DateUtils.nDaysAgo(1, new Date()), "yyyyMMdd")};
        String[] paySucCnt = {"0", "0", "0", "0", "0", "0", "0"};
        String[] paySucAmt = {"0", "0", "0", "0", "0", "0", "0"};
        String[] averagePrice = {"0", "0", "0", "0", "0", "0", "0"};
        for (int i = 0; i < txnDate.length; i++) {
            for (int j = 0; j < listDate.size(); j++) {
                Map mapDate = listDate.get(j);
                if (txnDate[i].equals(mapDate.get("STATISDATE") == null ? "yyyyMMdd" : mapDate.get("STATISDATE").toString())) {
                    paySucCnt[i] = mapDate.get("TOTALTRANSNUM") == null ? "0.00" : mapDate.get("TOTALTRANSNUM").toString();
                    paySucAmt[i] = mapDate.get("TOTALTRANSAMT") == null ? "0.00" : mapDate.get("TOTALTRANSAMT").toString();
                    averagePrice[i] = mapDate.get("AVGTRANSAMT") == null ? "0.00" : Float.valueOf(mapDate.get("AVGTRANSAMT").toString()).toString();
                }
            }
        }
        map.put("txnDate", txnDate);
        map.put("paySucCnt", paySucCnt);
        map.put("paySucAmt", paySucAmt);
        map.put("averagePrice", averagePrice);

//        for (Map.Entry<String, Object> entry : map.entrySet()) {
//            //Map.entry<Integer,String> 映射项（键-值对）  有几个方法：用上面的名字entry
//            //entry.getKey() ;entry.getValue(); entry.setValue();
//            //map.entrySet()  返回此映射中包含的映射关系的 Set视图。
//            System.out.println("key= " + entry.getKey() + " and value= "
//                    + entry.getValue());
//        }

        return map;
    }
}
